package com.AAA.service.Impl.mht;

import com.AAA.dao.mht.ITmhtPlaneMapper;
import com.AAA.dao.mht.ITmhtRecordDedtailMapper;
import com.AAA.dao.mht.ITmhtRecordMapper;
import com.AAA.entity.MyPage;
import com.AAA.entity.mht.TMhtPlane;
import com.AAA.entity.mht.TMhtRecord;
import com.AAA.service.mht.ITmhtPlaneService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.Id;
import java.util.Date;
import java.util.List;

@Service
public class TmhtPlaneServiceImpl implements ITmhtPlaneService {
    @Autowired
    private ITmhtPlaneMapper mapper;
    @Autowired
    private ITmhtRecordMapper recordMapper;
    @Override
    public PageInfo<TMhtPlane> queryall(MyPage myPage, TMhtPlane tMhtPlane) {
        PageHelper.startPage(myPage.getPage(),myPage.getLimit());
        List<TMhtPlane> tMhtPlanes = mapper.query(tMhtPlane);
        PageInfo pageInfo = new PageInfo(tMhtPlanes);
        return pageInfo;
    }

    @Override
    public boolean add(TMhtPlane tMhtPlane) {
        int i=0;
        if (tMhtPlane.getId()==null){
            tMhtPlane.setCreatetime(new Date());
            tMhtPlane.setUpdatetime(new Date());
             i = mapper.insert(tMhtPlane);
        }else {
            tMhtPlane.setUpdatetime(new Date());
            i = mapper.updateByPrimaryKeySelective(tMhtPlane);
        }

        if (i>0){
            return true;
        }
        return false;
    }

    @Override
    public int updatastatus(TMhtPlane tMhtPlane) {
        int i = mapper.updateByPrimaryKeySelective(tMhtPlane);
        if (i>0){
            return 0;
        }
        return 1;
    }

    @Override
    public boolean checkname(String planename) {
        TMhtPlane tMhtPlane = new TMhtPlane();
        tMhtPlane.setPlanename(planename);
        List<TMhtPlane> select = mapper.select(tMhtPlane);
        if (select.size() == 0){
            return false;
        }
        return true;
    }

    @Override
    public boolean checkcode(String code) {
        TMhtPlane tMhtPlane = new TMhtPlane();
        tMhtPlane.setCode(code);
        List<TMhtPlane> select = mapper.select(tMhtPlane);
        if (select.size() == 0){
            return true;
        }
        return false;
    }

    @Override
    public TMhtPlane getByCode(String code) {
        TMhtPlane tMhtPlane = new TMhtPlane();
        tMhtPlane.setCode(code);
        List<TMhtPlane> select = mapper.select(tMhtPlane);
        return select.get(0);
    }

    @Override
    public Integer delete(Integer id) {
        TMhtPlane tMhtPlane = mapper.selectByPrimaryKey(id);
        TMhtRecord tMhtRecord = new TMhtRecord();
        tMhtRecord.setYqcode(tMhtPlane.getCode());
        List<TMhtRecord> select = recordMapper.select(tMhtRecord);
        if (select.size()>0){
            return 0;
        }else {
            int i = mapper.deleteByPrimaryKey(id);
            if (i>0){
                return 1;
            }else {
                return 2;
            }
        }

    }
}
